function showAds() {
	mask.open(true);
	ajaxGet(website + "boAds/show", null, function(result) {
		var tabOrder = $("#currentTabOrder").val();
		$("#tabs-"+tabOrder).html(result);
		initAds();
		mask.close();
	}, "html");
}

function initAds() {
	$("#query-ads").button().click(function() {
		search();
	});
	
	$("#create-ads").button().click(function() {
		addOrEditAdsDialog(ACTION_ADD);
	});
	
	$("#edit-ads").button().click(function() {
		addOrEditAdsDialog(ACTION_EDIT);
	});

	$("#delete-ads").button().click(function() {
		openDeleteDialog(ADS,"checkKey");
	});
	
	$(".editAdsBtn").button().click(function() {
		addOrEditAdsDialog(ACTION_EDIT, $(this).attr("adsid"));
	});

	$(".delAdsBtn").button().click(function() {
		openDeleteDialog(ADS,"checkKey",$(this).attr("adsid"));
	});
}

function addOrEditAdsDialog(action, updateId) {
	var url = website + "boAds/insertPage";
	var params = null;
	if(action == ACTION_EDIT){
		var ids = null;
		if(updateId == null){
			ids = getSelectedItems("checkKey");
			if (ids == "" || ids.split(",").length != 1) {
				alert("请只选择一个广告");
				return;
			}
		}else{
			ids = updateId;
		}
		params = {
			adsid : ids
		}
		url = website + "boAds/updatePage";
	}
	
	$("#curAction").val(action);
	
	mask.open(true);
	ajaxGet(url, params, function(result) { 
		$("#dialog-form-ads").html(result);
		$("#dialog-form-ads") .dialog({
			width : 450,
			modal : true,
			resizable : false,
			buttons : {
				"确定" : function() {
					saveAds();
				},
				"取消" : function() {
					$(this).dialog("close");
				}
			},
			close : function() {
				allFields.val("").removeClass("ui-state-error");
				$(this).html("");
			}
		});
		mask.close();
	}, "html");
}


function saveAds(){
	var value = $("#value");
	var file = $("#file");
	var desc = $("#desc");
	var allFields = $([]).add(value).add(file).add(desc), tips = $(".validateTips");
	
	var bValid = true;
	allFields.removeClass("ui-state-error");
	bValid = bValid&& checkLength(value, "值",1, 100, tips);
	if($("#curAction").val() == ACTION_ADD){
		bValid = bValid&& checkLength(file, "文件",1, 100, tips);
	}
	if (bValid) {
		$("#adsForm").submit();
	}
}

function search(){
	searchWithPage();
}

function getPage(pageIndex){
	searchWithPage(pageIndex);
}

function searchWithPage(pageIndex){
	var categoryType = $("#categoryType").val();
	var searchText = $("#searchText").val();
	var params = null;
	if(pageIndex != null){
		params = {
			categoryType:categoryType,
			searchText:searchText,
			page:pageIndex
		}
	}else{
		params = {
			categoryType:categoryType,
			searchText:searchText
		}
	}
	mask.open(true);
	ajaxGet(website + "boAds/search",params,function(result){
		var tabOrder = $("#currentTabOrder").val();
		$("#tabs-"+tabOrder).html(result);
		initAds();
		mask.close();
	},"html");
}